package com.zzyk.microCloud.controller;

import com.github.pagehelper.PageInfo;
import com.zzyk.main.configuration.exception.UserException;
import com.zzyk.main.controller.BaseController;
import com.zzyk.main.model.pojo.Organization;
import com.zzyk.main.model.pojo.SysUser;
import com.zzyk.main.model.vo.Message;
import com.zzyk.main.model.vo.UserInfo;
import com.zzyk.main.service.ISysOrganizationService;
import com.zzyk.main.service.ISysRoleService;
import com.zzyk.main.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sys/user")
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysOrganizationService organizationService;

    /**
     * 获取用户列表 系统管理员可用或具备查询权限的可用
     *
     * @param userInfo 模糊查询用
     * @return 用户列表
     */
    @RequestMapping("list")
    public Message listUser(UserInfo userInfo) throws UserException {
        SysUser sysUser = currentUser();
        Organization org = organizationService.get(sysUser.getEnterprise());
        if (org == null) {
            throw new UserException("当前用户未加入任何组织机构");
        }
        List<UserInfo> list = null;
        setPage();
        if (sysUser.getReadonly() == 1) {
            list = sysUserService.getUserList(userInfo, 0);
        } else {
            if (org.getType() == 0) {
                userInfo.setArea(sysUser.getArea());
                list = sysUserService.getUserList(userInfo, 1);
            } else {
                userInfo.setEnterprise(sysUser.getEnterprise());
                list = sysUserService.getUserList(userInfo, 2);
            }
        }
        PageInfo<UserInfo> info = new PageInfo<>(list);
        return Message.success("用户查询成功")
                .addData("data", info.getList())
                .addData("total", info.getTotal())
                .addData("pages", info.getPages());
    }


    /**
     * 获取具体某个用户的信息 需要管理员权限或用户查询权限
     *
     * @param userId 用户的ID
     * @return 用户信息
     * @throws UserException 如果用户不存在
     */
    @RequestMapping("info")
    public Message findUser(@RequestParam("id") Integer userId) throws UserException {
        SysUser user = sysUserService.getUserWithRole(userId);
        if (user != null) {
            Message message = Message.success("用户查询成功");
            message.addData("data", user);
            return message;
        } else {
            throw new UserException("用户不存在");
        }
    }

    /**
     * 更新系统用户 因为框架本为多角色用户设计 所以这里需要需要将roleId另外传过来
     *
     * @param sysUser 系统用户
     * @param roleId  角色ID
     * @return 成功
     * @throws UserException 失败
     */
    @RequestMapping("save")
    @Transactional
    public Message saveUser(SysUser sysUser,
                            @RequestParam(value = "role", required = false) Integer roleId) throws UserException {
        if (sysUserService.updateUser(sysUser) > 0) {
            if (roleId != null) {
                sysRoleService.changeUserRole(sysUser.getId(), roleId);
            }
            if (currentUser().getUsername().equals(sysUser.getUsername())) {
                logout();
                return Message.success("修改成功, 请重新登录").setCode(302);
            } else {
                return Message.success("修改成功");
            }
        } else {
            return Message.success("用户未修改");
        }
    }

    /**
     * 删除系统用户
     *
     * @param id 用户id
     * @return 成功
     * @throws UserException 失败
     */
    @RequestMapping("delete")
    @Transactional
    public Message deleteUser(@RequestParam("id") Integer id) throws UserException {
        SysUser sysUser = sysUserService.getUser(id);
        if (sysUserService.deleteUser(id) > 0) {
            SysUser user = currentUser();
            if (user.getUsername().equals(sysUser.getUsername())) {
                logout();
            }
            return Message.success("删除成功");
        } else {
            throw new UserException("删除失败");
        }
    }

    /**
     * 根据企业类型获取用户列表
     *
     * @param orgType 企业类型 0企业 1银行
     * @return 用户列表
     */
    @RequestMapping("type")
    public Message getUserByOrgType(@RequestParam(value = "type", defaultValue = "0") Integer orgType) {
        return Message.success().addData("data", sysUserService.getUserByOrganizationType(orgType));
    }
}
